package com.jhs.kjs.robot.web.daum;

import com.jhs.kjs.robot.web.IMakeUrl;

public class DaumMakeUrl implements IMakeUrl {
	private String apikey;
	private StringBuilder requestUrl = new StringBuilder();
	private static final String daumLocalUrl = "http://apis.daum.net/local/geo/coord2addr";

	public DaumMakeUrl(String apikey) {
		this.apikey = apikey;
	}

	private String getApikey() {
		return apikey;
	}

	/**
	 * 
	 * @param lat
	 * @param lng
	 * @return 비정상적인 데이타일시 null 리턴
	 */
	public String getUrl(String lat, String lng) throws NumberFormatException,
			Exception {

		return getUrl(Double.parseDouble(lat), Double.parseDouble(lng));
	}

	/**
	 * 
	 * @param lat
	 * @param lng
	 * @return 비정상적인 데이타일시 null 리턴
	 */
	public String getUrl(Double lat, Double lng) throws NumberFormatException,
			Exception {
		requestUrl.setLength(0);

		// 한국을 벗어나는 위경도이면 null 리턴
		if (lat < 33 || lat > 39)
			return null;
		if (lng < 125 || lng > 132)
			return null;

		requestUrl.append(daumLocalUrl).append("?apikey=").append(getApikey())
				.append("&longitude=").append(lng).append("&latitude=")
				.append(lat)
				.append("&format=&output=json&inputCoordSystem=WGS84");

		return requestUrl.toString();
	}

}
